/*
 * @Author: 9ilk 3535851673@qq.com
 * @Date: 2024-11-27 20:11:24
 * @LastEditors: 9ilk 3535851673@qq.com
 * @LastEditTime: 2024-11-27 20:18:08
 * @FilePath: /zhuang/Cond/Cond.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#pragma once

#include <iostream>
#include <pthread.h>
#include "Mutex.hpp"
using namespace MutexModule;

namespace CondModule
{
    class Cond
    {
    public:
        Cond()
        {
            int n = ::pthread_cond_init(&_cond, nullptr);
            (void)n;
        }
        void Wait(Mutex &mutex) // 锁不能拷贝所以使用引用
        {
            // wait必须跟锁有关 要释放曾经持有的锁
            int n = ::pthread_cond_wait(&_cond, mutex.LockPtr());
            (void)n;
        }
        void Notify() // 通知
        {
            int n = ::pthread_cond_signal(&_cond);
            (void)n;
        }
        ~Cond()
        {
            int n = ::pthread_cond_destroy(&_cond);
        }

    private:
        pthread_cond_t _cond;
    };

}
